package practice1_100;

public class Practice654 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
       TreeNode root= dp(nums,0,nums.length-1 );
       return root;
    }
    public TreeNode dp(int[] nums,int begin,int end){
        if (begin>end) return null;
        int maxValue=Integer.MIN_VALUE;
        int index=0;
        for(int i=begin;i<=end;i++){
            index= nums[i]>maxValue?i:index;
            maxValue=maxValue>nums[i]?maxValue:nums[i];
        }
        TreeNode root=new TreeNode(maxValue);//卡在这里了，不能将TreeNode放在方法外面
        root.left=dp(nums,begin,index-1);//
        root.right=dp(nums,index+1,end);
        return root;
    }

    public static void main(String[] args) {
        //[3,2,1,6,0,5]
        int[] a=new int[] {3,2,1,6,0,5};
        TreeNode root= new Practice654().constructMaximumBinaryTree(a);
        System.out.println(root);

    }
}
